<?php

namespace app\v1\logic;

use app\v1\model\MiniFeedback;
use think\Db;

class MiniFeedbackLogic
{
    public MiniFeedback $model;

    public function __construct()
    {
        $this->model = new MiniFeedback();
    }

    public function save($param, $userId): array
    {
        if (!$userId) return [false, '请先登录'];

        $mobile = (new MiniUserLogic())->model->where('user_id', $userId)->value('mobile');
        if (!$mobile) return [false, '请先授权手机号码方便客服联系'];

        Db::startTrans();
        try {
            foreach ($param as $k => $v) {
                if (is_array($v)) {
                    $param[$k] = implode(',', $v);
                }
            }
            $param['user_id'] = $userId;
            $this->model->save($param);
            Db::commit();
            return [true, '提交成功'];
        } catch (\Throwable $t) {
            Db::rollback();
            return [false, [$t->getFile(), $t->getLine(), $t->getMessage()]];
        }
    }
}